package online.afeibaili.storebackendframework.util

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import online.afeibaili.storebackendframework.model.OrderEntity
import online.afeibaili.storebackendframework.model.ProductEntity
import online.afeibaili.storebackendframework.model.ShoppingCartEntity
import online.afeibaili.storebackendframework.model.json.ProductJsonMapper
import online.afeibaili.storebackendframework.service.impl.OrderServiceImpl
import online.afeibaili.storebackendframework.service.impl.ProductServiceImpl
import online.afeibaili.storebackendframework.service.impl.ShoppingCartServiceImpl
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import java.util.*


/**
 * 订单生成器
 *
 *@author AfeiBaili
 *@version 2025/10/20 18:13
 */

@Component
class OrderGenerator {
    @Autowired
    lateinit var productService: ProductServiceImpl

    @Autowired
    lateinit var orderServiceImpl: OrderServiceImpl

    @Autowired
    lateinit var shoppingCartServiceImpl: ShoppingCartServiceImpl

    val productTextList: List<String> =
        this::class.java.getResourceAsStream("/product.txt")!!.bufferedReader().readLines()

    val scope = CoroutineScope(Dispatchers.Default)

    fun generateProducts(count: Int) {

        val random = Random()
        repeat(count) {
            val text: String = productTextList[random.nextInt(productTextList.size)]
            val strings: List<String> = text.split("-")
            val entity: ProductEntity = ProductEntity().apply {
                createTime = System.currentTimeMillis()
                name = strings[1]
                price = strings[2].toDouble()
                category = strings[0]
            }
            println(productService.create(entity))
        }
    }

    @Transactional
    fun generateOrderCart(accountId: String, count: Int) {
        val random = Random()
        val entities: List<ProductEntity> = productService.getAll()
        repeat(count) {
            val itemCount: Int = random.nextInt(1, 5)
            val pairs: List<Pair<Int, ProductEntity>> = List(itemCount) {
                val count: Int = random.nextInt(1, 10)
                val entity: ProductEntity = entities[random.nextInt(entities.size)]
                Pair(count, entity)
            }

            val productJsonMappers: List<ProductJsonMapper> =
                pairs.map { ProductJsonMapper(it.second.createTime, it.first) }
            val json: String = ProductJsonMapper.toListJson(productJsonMappers)
            val shoppingCartEntity = ShoppingCartEntity(accountId, json)
            shoppingCartServiceImpl.create(shoppingCartEntity)

            val amount: Double = pairs.sumOf { it -> it.second.price * it.first }
            val orderEntity = OrderEntity(accountId, json, amount, amount, "", "")
            orderServiceImpl.create(orderEntity)
            println(json)
        }
    }
}